package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;

    /*
    * 1.判定是保存还是更新
    * 2.保存或者更新房间信息
    * 3.如果是更新，删除中间表数据
    * 4.最终一定进行中间表的数据保存
    * */
    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        //1.判定是保存还是更新
        boolean isUpdate = roomSubmitVo.getId() != null;
        //2.保存或者更新房间信息
        saveOrUpdate(roomSubmitVo);
        //3.如果是更新，删除中间表数据
        if (isUpdate){
            //图片
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            lambdaQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(lambdaQueryWrapper);
            //房间配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            //房间标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getLabelId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //房间支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getPaymentTypeId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //房间租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getLeaseTermId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        //4.最终一定进行中间表的数据保存
            //图片
            List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
            if (!CollectionUtils.isEmpty(graphVoList)){
                List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setItemId(roomSubmitVo.getId());
                    graphInfo.setItemType(ItemType.ROOM);
                    graphInfo.setName(graphVo.getName());
                    graphInfo.setUrl(graphVo.getUrl());
                    graphInfoList.add(graphInfo);
                }
                graphInfoService.saveBatch(graphInfoList);
            }
            //房间标签
            List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
                if (!CollectionUtils.isEmpty(labelInfoIds)){
                    List<RoomLabel> roomLabelList = new ArrayList<>(labelInfoIds.size());
                    for (Long labelInfoId : labelInfoIds) {
                        RoomLabel roomLabel = RoomLabel.builder().labelId(labelInfoId).roomId(roomSubmitVo.getId()).build();
                        roomLabelList.add(roomLabel);
                    }
                    roomLabelService.saveBatch(roomLabelList);
                }
            //房间配套
            List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
                if (!CollectionUtils.isEmpty(facilityInfoIds)){
                    List<RoomFacility> roomFacilityList = new ArrayList<>(facilityInfoIds.size());
                    for (Long facilityInfoId : facilityInfoIds) {
                        RoomFacility roomFacility = RoomFacility.builder().facilityId(facilityInfoId).roomId(roomSubmitVo.getId()).build();
                        roomFacilityList.add(roomFacility);
                    }
                    roomFacilityService.saveBatch(roomFacilityList);
                }
            //房间支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
                if (!CollectionUtils.isEmpty(paymentTypeIds)){
                    List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
                    for (Long paymentTypeId : paymentTypeIds) {
                        RoomPaymentType roomPaymentType = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomSubmitVo.getId()).build();
                        roomPaymentTypeList.add(roomPaymentType);
                    }
                    roomPaymentTypeService.saveBatch(roomPaymentTypeList);
                }
                //房间租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
                if (!CollectionUtils.isEmpty(leaseTermIds)){
                    List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>(leaseTermIds.size());
                    for (Long leaseTermId : leaseTermIds) {
                        RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomSubmitVo.getId()).build();
                        roomLeaseTermList.add(roomLeaseTerm);
                    }
                    roomLeaseTermService.saveBatch(roomLeaseTermList);
                }
        //房间属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
                if (!CollectionUtils.isEmpty(attrValueIds)){
                    List<RoomAttrValue> roomAttrValueList = new ArrayList<>(attrValueIds.size());
                    for (Long attrValueId : attrValueIds) {
                        RoomAttrValue roomAttrValue = RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomSubmitVo.getId()).build();
                        roomAttrValueList.add(roomAttrValue);
                    }
                    roomAttrValueService.saveBatch(roomAttrValueList);
                }

    }
}




